package com.ydd.controller.user;

import com.ydd.dto.FrontListDTO;
import com.ydd.dto.FrontRegDTO;
import com.ydd.result.Result;
import com.ydd.service.IndexService;
import com.ydd.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController("UserIndexController")
@Api(tags = "索引相关接口")
@RequestMapping("/index")
@Slf4j
@CrossOrigin
public class IndexController {


    @Autowired
    IndexService indexService;

    /**
     * 热门置顶文章数据(首页)
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/new")
    @ApiOperation("最近分页数据")
    @Cacheable(value = "newCache", key = "#page+#limit",unless = "#result==null") // 热点数据加入缓存
    public Result<FrontNewVO> newIndex(String page, String limit) {
        FrontNewVO frontNewVO = indexService.frontNewIndex(page, limit);
        return Result.success(frontNewVO);
    }


    /**
     * 首页和全局数据
     *
     * @return
     */
    @GetMapping("/index")
    @ApiOperation("首页和全局数据")
    @Cacheable(value = "listCache", key = "#root.methodName", unless = "#result==null") // 热点数据加入缓存
    public Result<FrontIndexVO> indexIndex() {
        FrontIndexVO frontIndexVO = indexService.frontIndexIndex();
        return Result.success(frontIndexVO);
    }

    /**
     * 文章列表数据
     *
     * @param frontListDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("文章列表数据")
    public Result list(FrontListDTO frontListDTO) {
        log.info("获取文章列表数据:{}", frontListDTO);
        indexlistDTO list = indexService.list(frontListDTO);
        FrontIndexListVO frontIndexListVO = new FrontIndexListVO();
        frontIndexListVO.setList(list);
        return Result.success(frontIndexListVO);
    }

    /**
     * 最新文章
     *
     * @return
     */
    @GetMapping("/hot")
    @ApiOperation("最热文章")
    @Cacheable(value = "listCache", key = "#root.methodName", unless = "#result==null") // 热点数据加入缓存
    public Result<FrontHotVO> hot() {
        log.info("获取最热文章");
        FrontHotVO frontHotVO = indexService.frontNew();
        return Result.success(frontHotVO);
    }

    /**
     * 推荐文章
     *
     * @return
     */
    @GetMapping("/recommend")
    @ApiOperation("侧边推荐文章")
    public Result<FrontHotVO> recommend() {
        log.info("获取侧边推荐文章");
        FrontHotVO frontHotVO = indexService.frontRecommend();

        return Result.success(frontHotVO);
    }


    /**
     * 文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/show")
    @ApiOperation("/文章详情数据")
    public Result<FrontShowVO> show(String id) {

        FrontShowVO frontShowVO = indexService.frontshow(id);

        return Result.success(frontShowVO);
    }


    /**
     * 用户注册
     * @param regDTO
     * @return
     */
    @PostMapping("/reg")
    @ApiOperation("用户注册")
    public Result<FrontRegVO> reg(@RequestBody FrontRegDTO regDTO) {
        log.info("用户注册:{}", regDTO);
        FrontRegVO frontRegVO = indexService.reg(regDTO);
        return Result.success(frontRegVO);
    }
}
